''' <summary>
''' This is a game component that implements IUpdateable.
''' </summary>
Public Class StartEndStates
    Inherits Microsoft.Xna.Framework.DrawableGameComponent
    Public Event StartCompleted()
    Public Event TimeoutCompleted()
    Public Event NextLevel()
    Private mblnShowReady As Boolean = False
    Private mblnEventEnded As Boolean = False
    Private mblnShowTimeOut As Boolean = False
    Private mblnEndOfLevel As Boolean = False
    Private mFont As SpriteFont
    Private mFontL As SpriteFont
    Private SpriteBatch As SpriteBatch
    Private mOpStart As TimeSpan
    Private mFadeRect As Texture2D
    Private mDeadMessages() As String = New String() {"Finger to screen interface error?", "Wow..... that was..... great :)", "At least you're happy in yourself.. right?", "It's only a game!", "Mission Status : Serious Fail!", "The miracle never happened", "YEEEEAAAAAAAAAAAAAAAAAAAAAH!", "Won't somebody think of the children!", "Error 0002921 : You Suck!", "Game over man.... GAME OVER!", "You win.... at losing!", "Yoda says.... Loser you are!", "ohhhh noooooooeeeessssss"}
    Private mstrGOMessages As String
    Private mcntOverLay As overlay

    Public Sub TimeOut()
        If (Not mblnShowTimeOut) Then
            Dim Generator As System.Random = New System.Random()
            mstrGOMessages = mDeadMessages(Generator.Next(0, mDeadMessages.Length - 1))
            mblnShowTimeOut = True
            mblnEventEnded = False
            mOpStart = TimeSpan.Zero
        End If
    End Sub
    Public Sub ReadyGo()
        If (Not mblnShowReady) Then
            mblnShowReady = True
            mblnEventEnded = False
            mOpStart = TimeSpan.Zero
        End If
    End Sub
    Public Sub EndOfLevel(ByRef cntOverlay As overlay)
        If (Not mblnEndOfLevel) Then
            mcntOverLay = cntOverlay
            mblnEndOfLevel = True
            mblnEventEnded = False
            mOpStart = TimeSpan.Zero
        End If
    End Sub
    Public Sub New(ByVal game As Game)
        MyBase.New(game)
    End Sub

    Public Overrides Sub Initialize()
        MyBase.Initialize()
    End Sub

    Protected Overrides Sub LoadContent()
        SpriteBatch = New SpriteBatch(GraphicsDevice)
        mFont = Game.Content.Load(Of SpriteFont)("motorwerk")
        mFontL = Game.Content.Load(Of SpriteFont)("motorwerklarge")
        MyBase.LoadContent()
    End Sub

    Public Overrides Sub Draw(gameTime As Microsoft.Xna.Framework.GameTime)
        Dim State As RenderStates = New RenderStates
        Static StartTimeBonus As Single = 0
        Static JumpBonus As Single = 0
        Static opEndLevel As TimeSpan = TimeSpan.Zero
        Static opShowJumps As TimeSpan = TimeSpan.Zero
        Static opNextJumpIn As TimeSpan = TimeSpan.Zero

        RecordRenderState(State, GraphicsDevice)
        If (mblnShowReady) Then
            If (mOpStart = TimeSpan.Zero) Then
                mOpStart = gameTime.TotalGameTime
            End If
            Dim Seconds As Double = (gameTime.TotalGameTime - mOpStart).TotalSeconds
            If (Seconds >= 1 And Seconds < 2) Then
                SpriteBatch.Begin()
                DrawWord(mFontL, "READY", CInt(GraphicsDevice.Viewport.Width / 2.0), CInt(GraphicsDevice.Viewport.Height / 2.0) - 50, Color.White, 1)
                SpriteBatch.End()
            End If
            If (Seconds >= 2 And Seconds < 3) Then
                SpriteBatch.Begin()
                DrawWord(mFontL, "SET", CInt(GraphicsDevice.Viewport.Width / 2.0), CInt(GraphicsDevice.Viewport.Height / 2.0) - 50, New Color(255, 255, 150), 1)
                SpriteBatch.End()
            End If
            If (Seconds >= 3 And Seconds < 4) Then
                If (Not mblnEventEnded) Then
                    mblnEventEnded = True
                    RaiseEvent StartCompleted()
                End If
                SpriteBatch.Begin()
                DrawWord(mFontL, "GO", CInt(GraphicsDevice.Viewport.Width / 2.0), CInt(GraphicsDevice.Viewport.Height / 2.0) - 50, Color.Yellow, 1)
                SpriteBatch.End()
            End If
            If (Seconds >= 4) Then
                mblnShowReady = False
            End If
        End If
        If (mblnShowTimeOut) Then
            If (mOpStart = TimeSpan.Zero) Then
                mOpStart = gameTime.TotalGameTime
                mFadeRect = Nothing
                mFadeRect = New Texture2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height)
                Dim colourData() = New Color(mFadeRect.Width * mFadeRect.Height - 1) {}
                For intI As Integer = 0 To colourData.Length - 1
                    colourData(intI) = Color.Black
                Next
                mFadeRect.SetData(colourData)
            End If
            Dim Seconds As Double = (gameTime.TotalGameTime - mOpStart).TotalSeconds
            Dim MilliSeconds As Double = (gameTime.TotalGameTime - mOpStart).TotalMilliseconds
            If (Seconds > 0 And Seconds < 30) Then
                SpriteBatch.Begin()
                DrawWord(mFontL, "GAME OVER", CInt(GraphicsDevice.Viewport.Width / 2.0), CInt(GraphicsDevice.Viewport.Height / 2.0) - 60, New Color(150, 150, 255), 1)
                DrawWord(mFont, mstrGOMessages, CInt(GraphicsDevice.Viewport.Width / 2.0), CInt(GraphicsDevice.Viewport.Height / 2.0), Color.Yellow, 1)
                SpriteBatch.End()
            End If
            If (Seconds > 5) Then
                Dim intAlpha As Integer = CInt(255.0 / 2000.0 * (MilliSeconds - 5000.0))
                If (intAlpha > 255) Then intAlpha = 255
                SpriteBatch.Begin()
                SpriteBatch.Draw(mFadeRect, New Vector2(0, 0), New Color(255, 255, 255, intAlpha))
                SpriteBatch.End()
            End If
            If (Seconds > 7) Then
                RaiseEvent TimeoutCompleted()
                mblnShowTimeOut = False
            End If
        End If
        If (mblnEndOfLevel) Then
            If (mOpStart = TimeSpan.Zero) Then
                mOpStart = gameTime.TotalGameTime
                StartTimeBonus = 0
                JumpBonus = 0
                opShowJumps = TimeSpan.Zero
                opEndLevel = TimeSpan.Zero
                opNextJumpIn = TimeSpan.Zero
            End If
            Dim Seconds As Double = (gameTime.TotalGameTime - mOpStart).TotalSeconds
            SpriteBatch.Begin()
            If (Seconds > 1) Then
                DrawWord(mFont, "Cheer up... you made it for the bonus!", CInt(GraphicsDevice.Viewport.Width / 2.0), 100, New Color(200, 200, 255), 1)
            End If
            If (Seconds > 2) Then
                If (mcntOverLay.Time > 0) Then
                    mcntOverLay.Time -= CSng(0.1)
                    StartTimeBonus += 100
                    mcntOverLay.Score += 100
                End If
                If (mcntOverLay.Time <= 0 And opShowJumps = TimeSpan.Zero) Then
                    mcntOverLay.Time = 0
                    opShowJumps = gameTime.TotalGameTime + TimeSpan.FromSeconds(2)
                End If
                DrawWord(mFont, "Time Bonus * 100 : " + StartTimeBonus.ToString, CInt(GraphicsDevice.Viewport.Width / 2.0), 150, Color.Yellow, 1)
                If (gameTime.TotalGameTime > opShowJumps And opShowJumps <> TimeSpan.Zero) Then
                    DrawWord(mFont, "Jump Bonus * 1000 : " + JumpBonus.ToString, CInt(GraphicsDevice.Viewport.Width / 2.0), 200, Color.Yellow, 1)
                    If (mcntOverLay.Jumps > 0) Then
                        If (gameTime.TotalGameTime > opNextJumpIn) Then
                            JumpBonus += 1
                            mcntOverLay.Jumps -= 1
                            mcntOverLay.Score += 1000
                            opNextJumpIn = gameTime.TotalGameTime + TimeSpan.FromMilliseconds(400)
                        End If
                    Else
                        If (opEndLevel = TimeSpan.Zero) Then
                            opEndLevel = gameTime.TotalGameTime + TimeSpan.FromSeconds(2)
                        End If
                    End If
                End If
            End If
            SpriteBatch.End()
            If (gameTime.TotalGameTime > opEndLevel And opEndLevel <> TimeSpan.Zero) Then
                RaiseEvent NextLevel()
                mblnEndOfLevel = False
            End If
        End If
        RestoreRenderState(State, GraphicsDevice)
        MyBase.Draw(gameTime)
    End Sub

    Protected Overrides Sub UnloadContent()
        mFont = Nothing
        mFontL = Nothing
        MyBase.UnloadContent()
    End Sub


    Public Overrides Sub Update(ByVal gameTime As GameTime)
        MyBase.Update(gameTime)
    End Sub

    ' //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ' Draws information where specified Justify 0 - Left, 1 - Centre, 2 - Right
    Private Sub DrawWord(ByVal font As SpriteFont, ByVal strItem As String, ByVal intX As Integer, ByVal intY As Integer, ByVal colour As Color, Optional ByVal Justify As Integer = 0)
        Dim location As Vector2 = New Vector2(intX, intY)
        If (Justify > 0) Then
            Dim Size As Vector2 = font.MeasureString(strItem)
            If (Justify = 1) Then
                location.X -= Size.X / 2
            Else
                location.X -= Size.X
            End If
        End If
        SpriteBatch.DrawString(font, strItem, New Vector2(location.X + 3, location.Y + 3), New Color(0, 0, 0, 150))
        SpriteBatch.DrawString(font, strItem, location, colour)
    End Sub
End Class
